import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;

import java.awt.Color;
import java.io.IOException;

import com.googlecode.grtframework.event.RangefinderEvent;
import com.googlecode.grtframework.event.RangefinderListener;
import com.googlecode.grtframework.rpc.USBRPC;
import com.googlecode.grtframework.sensor.RPCRangeFinder;
import com.googlecode.grtframework.util.USBUtil;
import com.googlecode.gunncs.graph.TimedLinePlot;

/**
 * Displays acceleration data
 * 
 * @author ajc
 * 
 */
public class RPCDistanceTest implements RangefinderListener {

	private TimedLinePlot lp;

	public RPCDistanceTest(RPCRangeFinder accel) {
		lp = new TimedLinePlot("distance", 1000);
		lp.addData("d", Color.GREEN, "v");
		lp.setYRangingFixed(0, 700);
		accel.addRangefinderListener(this);
	}

	public static void main(String[] args) {
		System.out.println(USBUtil.getPorts());
		USBRPC conn = null;
		try {
			conn = new USBRPC("/dev/ttyUSB0");
		} catch (NoSuchPortException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortInUseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		RPCRangeFinder r = new RPCRangeFinder(conn, 43);
		r.startListening();

		RPCDistanceTest t = new RPCDistanceTest(r);

		// RPCAccelerometer r = new RPCAccelerometer(conn,
		// new int[] { 66, 67, 68 });
		// r.startListening();
		// RPCAccelerationTest test = new RPCAccelerationTest(r);

	}

	@Override
	public void distanceChanged(RangefinderEvent ev) {
		lp.addPoint("d", ev.getDistance());
	}

	@Override
	public void startListening() {

	}

	@Override
	public void stopListening() {

	}

}
